//
//  QFTabBarController.swift
//  sinaWeibo
//
//  Created by apple on 16/2/26.
//  Copyright © 2016年 Qifeng Yan. All rights reserved.
//

import UIKit

class QFTabBarController: UITabBarController,QFTabBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        tabBar.tintColor = UIColor.orangeColor() // 设置全局颜色
        
        // 创建一个tabBar 将系统的 tabBar换成自定义的
        let mainTabBar = QFTabBar()
        mainTabBar.btnDelegate = self // 指定代理
        
        setValue(mainTabBar, forKey: "tabBar") // 因为系统的tabBar是只读的 所以要通过KVC来修改
        
        
        addChildViewControllers()
        
    }
    
    // MARK: - QFTabBarDelegate 实现加号按钮的代理方法 弹出控制器
    func tabBarAddButtonDidClick() {
        print("按钮被点击了")
    }
    
    // MARK: - 加载子控制器
    func addChildViewControllers() {
        // 首页
        let homeVC = QFHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        // 消息
        let messageVC = QFMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        // 占位控制器
        addChildViewController(UIViewController())
        
        // 发现
        let discoverVC = QFDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        // 我
        let profileVC = QFProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    // MARK: - 加载子控制器(抽取方法)
    func addChildViewController(vc: UIViewController,title: String, imageName: String) {
        // 设置控制器属性
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        // 设置选中图片
        let selectedImage = imageName + "_selected"
        vc.tabBarItem.selectedImage = UIImage(named: selectedImage)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        // 设置标题的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        
        
        // 添加到父控件
        addChildViewController(UINavigationController(rootViewController: vc))
    }

}
